1. /* sxfxxsub.cpp by K.Tsuru */
  2. // function ID = 508 BRADIX
  3. #ifndef SN_H
  4. #include "sn.h"
  5. #endif
  6. static const char* func = "XXSub";
  7. /********************************************************
  8. SDecimal class
  9. Provides m-n where both have the same sign and |m| > |n|.
  10. Signs are not examined for speed.
  11. *********************************************************/
  12. void XXSub(const SDecimal& m, const SDecimal& n, SDecimal& result)
  13. {
  14. int msign = m.Sign(), nsign = n.Sign(), sgn = msign*nsign;
  15. if(sgn == 0){
  16. if(msign) result = m;
  17. else {
  18. result = n; result.SetSign(-nsign); // result = -n
  19. }
  20. return;
  21. }
  22. // m and n must have the same sign and the same size.
  23. if(sgn < 0) m.SetError(m.SIGN_ERR, func, 508);
  24. if( (m.figure.size() != n.figure.size())
  25. || (result.figure.size() != n.figure.size()) || (m.aTail > n.aTail) )
  26. m.SetError(m.SYNTAX_ERR, func, 508);
  27. uint rh = max(m.Head(), n.Head()); // aHead --> Head() since ver 2.191
  28. uint rt = m.aTail; // m >= n
  29. fType* rv = result.figure.Elements();
  30. const fType* mv = m.ReadFigures();
  31. const fType* nv = n.ReadFigures();
  32. #ifndef NDEBUG
  33. result.figure(rh);
  34. #endif
  35. fType u = 0;
  36. for(int i = rh; i >= (int)rt; i--) {
  37. u = mv[i] - nv[i] - u;
  38. rv[i] = u & BRADIX1;
  39. u = u >> BRADIX_BITS;
  40. }
  41. // rv[-1] = u;
  42. if(u) m.SetError(m.SYNTAX_ERR, func, 508); // m < n
  43. //Clear the outside of above roop.
  44. if(result.aHead > rh) result.figure.clear(rh+1, result.aHead);
  45. if(result.aTail < rt) result.figure.clear(result.aTail, rt-1);
  46. //Decide the figure position.
  47. uint j = rt;
  48. while( !rv[j] && (j < rh) ) j++;
  49. if(j == rh){
  50. result.aTail = result.aHead = rh;
  51. result.SetSign(rv[rh] ? msign : 0);
  52. } else {
  53. result.aTail = j;
  54. while(!rv[rh]) rh--; //There is rv[rh] != 0 anywhere.
  55. result.aHead = rh;
  56. result.SetSign(msign);
  57. }
  58. }

sxfxxsub.cpp : last modifiled at 2017/03/13 14:32:02(1,841 bytes)
created at 2015/12/22 16:09:56
The creation time of this html file is 2017/10/27 15:45:59 (Fri Oct 27 15:45:59 2017).